export default {
    // 开启命名空间
    namespaced: true,

    // state 数据
    state: () => ({
        // 待售书籍的数组
        // {
        //             "book_id": 1,
        //             "book_ISBN":9787501615001,
        //             "book_name": "草鞋湾1",
        //             "book_price": 3.15,   
        //             "book_state": false,   // 删除的勾选状态 /默认为false
        //             "sell_state": number,  // 加入代售清单状态 0/待加入清单    1/已加入清单    2/暂不回收 默认为0
        //             "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png"     //图书的图片
        //             "sell_state": 0,   // 是否添加到卖书清单   0 /未添加   1/已添加
        //             "book_count":1  // 书本数量默认为1
        //             "book_fascicle":false   //书本是否分册 默认为false/没有分册
        //  },
        book: JSON.parse(uni.getStorageSync("book") || "[]"),
        sellbook: JSON.parse(uni.getStorageSync("sellbook") || "[]"),
        databook: JSON.parse(uni.getStorageSync("databook") || "[]")

    }),
    // 方法
    mutations: {
        //收书的数据库
        saveToStorage_databook(state) {
            state.databook = [{
                    "book_id": 1,
                    "book_ISBN": 9787501615001,
                    "book_name": "草鞋湾1",
                    "book_price": 3.15,
                    "book_state": false,
                    "sell_state": 0,
                    "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png",
                    "book_count": 1,
                    "book_fascicle": false
                },
                {
                    "book_id": 2,
                    "book_ISBN": 9787501615002,
                    "book_name": "草鞋湾2",
                    "book_price": 3.15,
                    "book_state": false,
                    "sell_state": 0,
                    "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png",
                    "book_count": 1,
                    "book_fascicle": false
                },
                {
                    "book_id": 3,
                    "book_ISBN": 9787501615003,
                    "book_name": "草鞋湾3",
                    "book_price": 3.15,
                    "book_state": false,
                    "sell_state": 2,
                    "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png",
                    "book_count": 1,
                    "book_fascicle": false
                },
                {
                    "book_id": 4,
                    "book_ISBN": 9787501615004,
                    "book_name": "草鞋湾4",
                    "book_price": 3.15,
                    "book_state": false,
                    "sell_state": 0,
                    "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png",
                    "book_count": 1,
                    "book_fascicle": false
                },
                {
                    "book_id": 5,
                    "book_ISBN": 9787501615005,
                    "book_name": "草鞋湾5",
                    "book_price": 3.15,
                    "book_state": false,
                    "sell_state": 0,
                    "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png",
                    "book_count": 1,
                    "book_fascicle": true
                },
                {
                    "book_id": 6,
                    "book_ISBN": 9787532547555,
                    "book_name": "中国历代文学作品选中篇",
                    "book_price": 15,
                    "book_state": false,
                    "sell_state": 0,
                    "book_img": "https://static.botue.com/ugo/uploads/pic_floor01_title.png",
                    "book_count": 1,
                    "book_fascicle": false
                }
            ];
            uni.setStorageSync("databook", JSON.stringify(state.databook));
        },
        // 将扫描书籍中的数据持久化存储到扫描书籍本地
        saveToStorage_book(state) {
            uni.setStorageSync("book", JSON.stringify(state.book));
        },
        // 将待售书籍添加到本地存储
        saveToStorage_sellbook(state) {
            uni.setStorageSync("sellbook", JSON.stringify(state.sellbook));
        },
        //不回收提示框
        notsellTip() {
            return uni.showModal({
                title: "提示",
                content: "这本书暂不回收，请您过段时间再来试试~",
                confirmText: "好吧",
                showCancel: false,
                success: (res) => {
                    if (res.confirm) {
                        console.log("用户点击确定");
                    }
                }
            });
        },
        //添加到扫过的书籍信息中
        addToBook(state, result) {
            const findResult = state.databook.find((x) => x.book_ISBN == result);
            if (findResult) {
                if (findResult.sell_state === 2) {
                    this.commit("sellBookStore/notsellTip");
                    if (state.book.find((x) => x.book_id === findResult.book_id)) {
                        return;
                    } else {
                        state.book.push(findResult);
                        this.commit("sellBookStore/saveToStorage_book");
                    }
                } else if (findResult.sell_state === 1) {
                    return uni.showModal({
                        title: "提示",
                        content: "这本书已经在清单中了~",
                        confirmText: "确定",
                        showCancel: false,
                        success: (res) => {
                            if (res.confirm) {
                                console.log("用户点击确定");
                            }
                        }
                    });
                } else {
                    if (findResult.book_fascicle) {
                        uni.showModal({
                            // 弹出提示框,判断分册是否齐全
                            title: "需要确认你手里的书册数是否齐全",
                            content: `书名为：` + findResult.book_name + `	ISBN码为：` + findResult.book_ISBN,
                            confirmText: "确认齐全",
                            cancelText: "书册不全",
                            success: (res) => {
                                if (res.confirm) {
                                    uni.showModal({
                                        title: "温馨提示",
                                        content: "套装书(ISBN码相同的系列书籍)只需要扫描一册，扫码价即套餐价。打包时请把所有单册绑在一起或放在一个袋子里寄出。",
                                        confirmText: "确定",
                                        showCancel: false,
                                        success: (res2) => {
                                            if (res2.confirm) {
                                                state.book.push(findResult);
                                                this.commit("sellBookStore/saveToStorage_book");
                                                findResult.sell_state = 1;
                                                state.sellbook.push(findResult);
                                                this.commit(
                                                    "sellBookStore/saveToStorage_sellbook"
                                                );
                                            }
                                        }
                                    });
                                } else if (res.cancel) {
                                    uni.showModal({
                                        title: "提示",
                                        content: "这套书缺册不收，请不要邮寄过来。寄来会被拒收，且自行承担运费。",
                                        confirmText: "确定",
                                        showCancel: false,
                                        success: (res2) => {
                                            if (res2.confirm) {
                                                return;
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    } else {
                        state.book.push(findResult);
                        this.commit("sellBookStore/saveToStorage_book");
                        findResult.sell_state = 1;
                        state.sellbook.push(findResult);
                        this.commit("sellBookStore/saveToStorage_sellbook");
                    }
                }
            } else {
                this.commit("sellBookStore/notsellTip");
            }
        },
        // 加入卖书清单
        addToCart(state, book) {
            const findResult = state.book.find((x) => x.book_id === book.book_id);
            findResult.sell_state = 1;
            state.sellbook.push(findResult);
            this.commit("sellBookStore/saveToStorage_book");
            this.commit("sellBookStore/saveToStorage_sellbook");
        },
        //移除待售藏书
        removeBook(state) {
            state.book = state.book.filter((x) => !x.book_state);
            this.commit("sellBookStore/saveToStorage_book");
        },
        // //移除卖书清单的信息
        removeSellbook(state, sell) {
            state.sellbook = state.sellbook.filter((x) => x.book_id != sell.book_id);
            this.commit("sellBookStore/saveToStorage_sellbook");
            const findResult = state.book.find((x) => x.book_id === sell.book_id);
            if (findResult) {
                findResult.sell_state = 0;
                findResult.book_count = 1;
                this.commit("sellBookStore/saveToStorage_book");
            }
        },
        // 待售书籍的数量增加
        addbookCount(state, numBook) {
            const findResult = state.sellbook.find((x) => x.book_id === numBook.book_id);
            findResult.book_count = numBook.num;
            this.commit("sellBookStore/saveToStorage_sellbook");
        }
    },
    // 数据包装器
    getters: {
        // 统计已勾选的商品的总价
        sellBooksMoney(state) {
            return state.sellbook.reduce((total, item) => total += item.book_count * item.book_price, 0).toFixed(2);
        },
        // 统计勾选的商品的总数量
        sellBooksCount(state) {
            return state.sellbook.reduce((total, item) => total += item.book_count, 0);
        }

    },
}